package com.qzy.reggie.controller;

import com.qzy.reggie.common.PageResult;
import com.qzy.reggie.common.Result;
import com.qzy.reggie.pojo.Employee;
import com.qzy.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping()
    public Result list(Integer page,Integer pageSize,String name){
        PageResult pageResult = employeeService.queryEmployeeList(page,pageSize,name);
        return Result.success("查询成功",pageResult);
    }

//    @GetMapping()
//    public Map<String, Object> list(){
//        List<Employee> employees = employeeService.queryEmployeeList();
//
//        Map<String,Object> result = new HashMap<>();
//        result.put("data",employees);
//        result.put("code",200);
//        result.put("msg","查询成功");
//        return result;
//    }


//    @PostMapping("/login")
//    public Map<String, Object> login(@RequestBody Employee emp, HttpSession session){
//        Map<String,Object> result = new HashMap<>();
//        //1.根据用户名查询
//        Employee employee = employeeService.queryEmployeeByUserName(emp.getUsername());
//        //2.如果查询不到，返回登陆失败
//        if (employee == null){
//            result.put("code",200);
//            result.put("msg","登陆失败,用户名错误");
//            return  result;
//        }
//        //3.如果查询到。比较此密码（MD5加密后的密码）
//        String pwd = employee.getPassword();
//        String md5password = DigestUtils.md5DigestAsHex(emp.getPassword().getBytes());
//        //4.如果密码不一致，返回登陆失败
//        if (!pwd.equals(md5password)){
//            result.put("code",400);
//            result.put("msg","登陆失败");
//            return  result;
//        }
//        //5.如果密码一致，则判断员工状态是否正常
//        //6.如果不正常，返回登陆失败
//        if (employee.getStatus() != 1){
//            result.put("code",400);
//            result.put("msg","登陆失败：该员工已经禁止工作");
//            return  result;
//        }
//        //7.如果正常，则将员工id放入Session，返回成功结果
//        session.setAttribute("empId",employee.getId());
//        result.put("data",employee);
//        result.put("code",1);
//        result.put("msg","登录成功");
//        return result;
//    }

    @PostMapping("/login")
    public Result login(@RequestBody Employee emp, HttpSession session){
        //1.根据用户名查询
        Employee employee = employeeService.queryEmployeeByUserName(emp.getUsername());
        //2.如果查询不到，返回登陆失败
        if (employee == null){
            return  Result.error("登陆失败,用户名错误");
        }
        //3.如果查询到。比较此密码（MD5加密后的密码）
        String pwd = employee.getPassword();
        String md5password = DigestUtils.md5DigestAsHex(emp.getPassword().getBytes());
        //4.如果密码不一致，返回登陆失败
        if (!pwd.equals(md5password)){
            return  Result.error("登陆失败,密码错误");
        }
        //5.如果密码一致，则判断员工状态是否正常
        //6.如果不正常，返回登陆失败
        if (employee.getStatus() != 1){
            return  Result.error("登陆失败：该员工已经禁止工作");
        }
        //7.如果正常，则将员工id放入Session，返回成功结果
        session.setAttribute("empId",employee.getId());
        return Result.success("登录成功",employee);
    }

//    @PostMapping("/logout")
//    public Map<String,Object> logout(HttpSession session){
//        Map<String,Object> result = new HashMap<>();
//
//        session.removeAttribute("empId");
//        result.put("data",null);
//        result.put("code",1);
//        result.put("msg","退出成功");
//        return result;
//    }

    @PostMapping("/logout")
    public Result logout(HttpSession session){

        session.removeAttribute("empId");

        return Result.success("退出成功");
    }

    @PostMapping()
    public Result save(@RequestBody Employee employee,HttpSession session){

        Long empId = (Long) session.getAttribute("empId");

        // 完善系统信息
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        // 设置默认密码
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);

        Integer ret = employeeService.save(employee);

        if (ret > 0){
            return Result.success("新增成功");
        }else{
            return Result.error("新增失败");
        }
    }

    @GetMapping("{id}")
    public Result getEmpById(@PathVariable Long id){

        return Result.success(employeeService.queryEmployeeById(id));

    }

    @PutMapping()
    public Result updateEmp(@RequestBody Employee employee,HttpSession session){
        Long empId = (Long) session.getAttribute("empId");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);

        Integer ret = employeeService.update(employee);
        if (ret > 0){
            return Result.success("修改成功");
        }else{
            return Result.error("修改失败");
        }
    }

}
